<?php

namespace App\Jobs;

use App\Models\Common\Common;
use App\Models\Tools\Data;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ExploreAcceptJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $work_id;

    /**
     * Create a new job instance.
     *
     * @param int $work_id
     * @return void
     */
    public function __construct($work_id)
    {
        $this->work_id = $work_id;
    }

    /**
     * Execute the job.
     * 接单后3小时提醒可以投稿
     * @return void
     */
    public function handle()
    {
        try{
            $work = DB::table('explore_work as ew')
                ->leftJoin('shop','ew.shop_id','=','shop.shop_id')
                ->leftJoin('lottery_users as lu','ew.receiver_we_id','=','lu.we_id')
                ->where('ew.work_id','=',$this->work_id)
                ->select('ew.status','lu.openid','shop.shop_name')
                ->first();
            $work = json_decode(json_encode($work), true);
            // 如果工单不已接单状态直接结束
            if($work['status'] === Data::DATA_DB_EXPLORE_WORK_STATUS_INIT){
                $data = [
                    'first' => '您好，您在店铺'.$work['shop_name'].'的接单已可以开始投稿',
                    'keyword1' => '点评补发任务',
                    'keyword2' => '已通过',
                    'keyword3' => date('Y-m-d H:i:s'),
                    'remark' => [
                        'value' => '请在接单后的48小时内提交投稿，超时订单将自动取消。',
                        'color' => '#F00'
                    ],
                ];
                Common::sendLaifeiMessage($work['openid'],'passed',$data,'/talent/history?tab=1');
            }
        }catch (\Exception $e){
            Log::emergency(ExploreApplyJob::class,['work_id'=>$this->work_id,'msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return; // 异常状态提前结束
        }
        return;
    }
}
